<?php
require_once 'Core/Services/Abstract.php';

/**
 * The user services handler. Much like the controller, except without all the added mumbo-jumbo.
 */
class Core_Services_User
	extends Core_Services_Abstract
{
	/**
	 * Returns the current user.
	 * 
	 * @return Core_VO_User
	 */
	public function read ( )
	{
		if ( Zend_Auth::getInstance()->hasIdentity() )
		{
			$user = Core_Services_User_VO::fromArray(Core_User::getCurrentUser());
			return $user;
		}
		else
		{
			return null;
		}
	}
	
	/**
	 * Updates the current user.
	 * 
	 * @param Core_VO_User $user
	 * @return Core_VO_User
	 */
	public function update ( $user )
	{
		$currentUser = Core_User::getCurrentUser();
		
		if ( Zend_Auth::getInstance()->hasIdentity() && $user->id == $currentUser->id )
		{
			$currentUser->firstName = $user->firstName;
			$currentUser->lastName = $user->lastName;
			$currentUser->email = $user->email;
			$currentUser->news = $user->news;
			$currentUser->spam = $user->spam;
			$currentUser->save();
			
			return $currentUser->toServiceVO();
		}
		else
		{
			throw new Exception('Page not found', 404);
		}
	}
}
